
package ttOrder.service;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

//import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.servlet.ModelAndView;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;
import ttOrder.dao.inter.DinnerTableMapper;
import ttOrder.dao.inter.UserMapper;
import ttOrder.model.po.DinnerTable;
import ttOrder.model.po.OrderDetail;
import ttOrder.model.po.User;
import ttOrder.model.vo.DinnerTableVO;
import ttOrder.model.vo.OrderDetailVO;

@Service
public class DinnerTableServiceImpl extends abstractService<DinnerTable>{
	
	private final String Vacant = "空闲中";
	private final String Dining = "用餐中";
	private final String booking = "预定中";
	
	@Autowired
	private DinnerTableMapper dinnerTableMapper;
	
	/* operate with vo class */
	
	public int changeTableStatus(int id, int status){
		DinnerTable po = getByKey(new DinnerTable(id));
		po.setTableStatus(status);
		return update(po);
	}
	
	public List<DinnerTableVO> getAllVO() {
		return trunListPoToVo(dinnerTableMapper.selectAll());
	}
	
	public DinnerTableVO getVOByKey(DinnerTable record) {
		DinnerTableVO vo = null;
		if (record != null) {
			DinnerTable po = dinnerTableMapper.selectByPrimaryKey(record);
			if (po != null) {
				vo = new DinnerTableVO();
				BeanUtils.copyProperties(po, vo);
			}
		}
		
		return vo;
	}
	
	
	/* operate with po class */
	
	@Override
	public List<DinnerTable> getAll() {
		return dinnerTableMapper.selectAll();
	}
	
	@Override
	public List<DinnerTable> getByEntity (DinnerTable record){
		return dinnerTableMapper.select(record);
	}
	
	@Override
	public DinnerTable getByKey(DinnerTable record) {
		return dinnerTableMapper.selectByPrimaryKey(record);
	}
	
	@Transactional
	@Override
	public int insert (DinnerTable record){
		return dinnerTableMapper.insert(record);
	}
	
	@Transactional
	@Override
	public int update (DinnerTable record){
		return dinnerTableMapper.updateByPrimaryKeySelective(record);
	}
	
	@Transactional
	@Override
	public int deleteByKey (DinnerTable record){
		return dinnerTableMapper.deleteByPrimaryKey(record);
	}

	@Transactional
	@Override
	public int deleteByEntity(DinnerTable record) {
		return dinnerTableMapper.delete(record);
	}

	@Override
	public int getCount(DinnerTable record) {
		return dinnerTableMapper.selectCount(record);
	}
	
	/**
	 * 根据当前实体类的tableStatus字段获取实体类集合
	 * @author emoker 
	 * @see selectByExample(T t)
	 * */
	public List<DinnerTable> getTableByStatus(DinnerTable record) {
		Example e = new Example(DinnerTable.class); 
		Example.Criteria criteria = e.createCriteria();
		if (record.getTableStatus() >= 0) {
			criteria.andEqualTo("tableStatus", record.getTableStatus());
		}
		return dinnerTableMapper.selectByExample(e);
	}

	List<DinnerTableVO> trunListPoToVo(List<DinnerTable> polist){
		List<DinnerTableVO> volist = new ArrayList<DinnerTableVO>();
		if (polist.size() > 0) {
			DinnerTableVO vo = null;
			for (DinnerTable po : polist) {
				vo = new DinnerTableVO();
				BeanUtils.copyProperties(po, vo);
				if (po.getTableStatus()==1) {
					vo.setTableStatusString(Dining);
				}else  if (po.getTableStatus()==0) {
					vo.setTableStatusString(Vacant);
				}else if (po.getTableStatus()==2) {
					vo.setTableStatusString(booking);
				}
				volist.add(vo);
			}
		}
		return volist;
	}
	
}
